﻿ 
var $copyBtn = $('#btnCopyImg');

var meter1;
$(function () {
    //$('#time1').mtimer({
    //    timeStart: 0,
    //    timeNum :24
    //});
    // Basic usage time demo script

    // Mobiscroll Date & Time initialization
    //$('time1').mobiscroll().time({
    //    theme: 'mobiscroll',
    //    lang: 'zh',
    //    display: 'bottom',
    //    select: 'multiple',
    //    controls: ['calendar'],
    //    // mode: $('#mode').val(), // More info about mode: https://docs.mobiscroll.com/3-0-0_beta5/datetime#!opt-mode
    //    // headerText: false, // More info about headerText: https://docs.mobiscroll.com/3-0-0_beta5/datetime#!opt-headerText
    //    //  maxWidth: 90 // More info about maxWidth: https://docs.mobiscroll.com/3-0-0_beta5/datetime#!opt-maxWidth
    //});


    $('#time1').mobiscroll().time({
        theme: 'mobiscroll',
        lang: 'zh',
        display: 'bottom',
        select: 'multiple',

    });
    $('#time2').mobiscroll().time({
        theme: 'mobiscroll',
        lang: 'zh',
        display: 'bottom',
        select: 'multiple',

    });
    $('#time3').mobiscroll().time({
        theme: 'mobiscroll',
        lang: 'zh',
        display: 'bottom',
        select: 'multiple',

    });
    $('#time4').mobiscroll().time({
        theme: 'mobiscroll',
        lang: 'zh',
        display: 'bottom',
        select: 'multiple',

    });
    $('#time5').mobiscroll().time({
        theme: 'mobiscroll',
        lang: 'zh',
        display: 'bottom',
        select: 'multiple',

    });
    $('#time6').mobiscroll().time({
        theme: 'mobiscroll',
        lang: 'zh',
        display: 'bottom',
        select: 'multiple',

    });


    var flockNames = "";
    $("input[name=flockname]").each(function () {
        //flockIds += $(this).val() + ",";
        flockNames += $(this).attr("data-name") + ",";
        $('#flockNames').val(flockNames);
        //$('#flockIds').val(flockIds);
    });
    $("#btnSend").click(function () {

        //var flockIds = "";

        //if (flockIds == '') {

        //    layer.open({
        //        content: '请选要发单的群',
        //        style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
        //        time: 2,
        //        shade: false
        //    });
        //    return false;
        //}

        var FlockSendSecond = $('#FlockSendSecond').val();
        if (FlockSendSecond == '') {
            $('#FlockSendSecond').focus();
            layer.open({
                content: '请输入单与单的间隔时间',
                style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                time: 2,
                shade: false
            });
            return false;
        }


        var data = $('form').serializeArray();
        $.post(
            '/AgentCenter/PostWYFD',
            data,
            function (res) {
                layer.open({
                    content: res.Message,
                    style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                    time: 2,
                    shade: false
                });
                setTimeout(" location.reload()", 2000);
            },
            'json'
        );
    })


    $('#btnImg').click(function () {


        GetWXLoginImg();


    })

    $('#btnRefreshLogin').click(function () {
        clearTimeout(meter1);
        i = 0;
        IsRefresh = true;
        GetWXLoginImg();

       // $('#btnRefreshLogin').attr("data-isShow", "false");
        $('#btnRefreshLogin').hide();

        //window.setTimeout(function () {
        //    $('#btnRefreshLogin').attr("data-isShow", "true");
        //    $('#btnRefreshLogin').show();
        //}, 5000);


    })
    $('#btnRefreshImg').click(function () {
        clearTimeout(meter1);
        GetWXLoginImg();

         $('#btnRefreshImg').attr("disabled", "disabled");
        
        $("#btnRefreshImg").css("background", "#A9A9A9");

        window.setTimeout(function () {
            $("#btnRefreshImg").removeAttr("disabled");
          
            $("#btnRefreshImg").css("background", "rgb(254, 79, 63)");
            //$('#btnRefreshImg').show();
        }, 5000);

    });


    //点击复制
    $copyBtn.on('click', function () {
        var clipboard = new Clipboard('#btnCopyImg', {
            text: function (trigger) {
                return $copyBtn.data("clipboard-text");
            }
        });
        clipboard.on('success', function (e) {
            $copyBtn.html("已复制") 
        });

    });
   


})

function randomNum(n) {
    var t = '';
    for (var i = 0; i < n; i++) {
        t += Math.floor(Math.random() * 10);
    }
    return t;
}

var i = 0;
var IsRefresh = false;

function GetWXLoginImg() {

    $('#btnImg').hide();

    $('#wxLoginImg').attr('src', '/images/loading.gif')

    $('#imgMsg').html("");
    $('#imgMsg').hide();
    $('#wxLoginImg').show();
    $('#tishi').show();
 
   
    $('#btnCopyImg').show();

    
    $('#btnRefreshImg').show();
 
   



    $.post(
           '/AgentCenter/GetWXLoginImg',
          { flockNames: $('#flockNames').val(), IsRefresh: IsRefresh },
           function (res) {
               if (res.State) {
                   IsRefresh = false;
                   // alert("i"+i+" state:"+res.Datas)
                   initWXLogin(res.Datas);
               }
               else {
                   layer.open({
                       content: res.Message,
                       style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                       time: 2,
                       shade: false
                   });
                   //setTimeout(" location.reload()", 2000);
               }
           },
           'json'
       );
    meter1 = setTimeout("GetWXLoginImg()", 10000);
    if (i >= 20) {
        clearTimeout(meter1);
    }
    i++;
}



function initWXLogin(state){


    switch (state) {

        case "-1":
            

            
            $('#btnCopyImg').hide();
            $('#imgMsg').hide();
            
            $('#wxLoginImg').hide();
            $('#btnRefreshLogin').hide();
            
            $('#tishi').hide();
            $('#btnRefreshImg').hide();
            $('#rCode').show();
            $('#btnImg').show();
            break;
        case "1":
            //clearTimeout(meter1);
            $('#imgMsg').html("正在扫码...请等待");
            $('#btnCopyImg').hide();
            $('#imgMsg').show();
            $('#btnImg').hide();
            $('#wxLoginImg').hide();
            $('#btnRefreshLogin').show();
            $('#rCode').show();
            $('#tishi').hide();
            $('#btnRefreshImg').hide();
            break;
        case "2":
            clearTimeout(meter1);
            $('#imgMsg').html("扫码失败，请重新登陆！");
            $('#btnCopyImg').hide();
            $('#imgMsg').show();
            $('#btnImg').hide();
            $('#wxLoginImg').hide();
            $('#btnRefreshLogin').show();
            $('#rCode').show();
            $('#tishi').hide();
            $('#btnRefreshImg').hide();
            break;
        case "3":
            //clearTimeout(meter1);
            $('#imgMsg').html("加载数据...请等待");
            $('#btnCopyImg').hide();
            $('#imgMsg').show();
            $('#btnImg').hide();
            $('#wxLoginImg').hide();
            $('#btnRefreshLogin').show();
            $('#rCode').show();
            $('#tishi').hide();
            $('#btnRefreshImg').hide();
            break;
        case "4":
            clearTimeout(meter1);
            $('#imgMsg').html("web微信已登陆");
            $('#btnCopyImg').hide();
            $('#imgMsg').show();
            $('#btnImg').hide();
            $('#wxLoginImg').hide();
            $('#btnRefreshLogin').show();
            $('#rCode').show();
            $('#tishi').hide();
            $('#btnRefreshImg').hide();
            break;
        default:
            $('#btnImg').hide();
            $('#btnRefreshLogin').hide();
            $('#wxLoginImg').attr('src', state + "?l=" + randomNum(5));
            //$('#btnCopyImg').attr('clipboard-text', res.Datas + "?l=" + randomNum(5));
            $copyBtn.data('clipboard-text', state + "?l=" + randomNum(5));

           
          
            $('#imgMsg').html("");
            $('#btnCopyImg').show();
           
           
            $('#wxLoginImg').show();

            //if ($('#btnRefreshLogin').attr("data-isShow") == "true")
             
            $('#btnRefreshImg').show();
            $('#rCode').show();
            $('#tishi').show();
            break;
    }

}
